﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class FeedGenerator : ICPAdminPage
{
    public DataTable m_dtSource = new DataTable();

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Feed Generator";
        if (!IsPostBack && !IsCallback)
        {
        }

        InitGrid(false);
    }
    protected void cbkEditor_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = Handle_Editor(e.Parameter);
    }
    protected void dgMain_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        InitGrid(true);
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        Cached.FeedGenerator ccTmp = new Cached.FeedGenerator();
        m_dtSource = ccTmp.GetFeedGeneratorList(bRefresh);

        dgMain.Caption = "ICP Feed Generator List";

        dgMain.DataSource = m_dtSource;
        dgMain.DataBind();
    }

    private String Handle_Editor(String sxParameter)
    {
        String sxReturn = String.Empty;
        DataTable dt = null;
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        int iInputId = int.Parse(sxSplitted[1]);

        switch (sxSplitted[0].ToUpper())
        {
            case "ADD":
                if (DBUtils.IsQueryValid(sxSplitted[2], sxSplitted[4], ref dt))
                {
                    sxReturn = VerifyTable(dt);

                    if (String.IsNullOrEmpty(sxReturn))
                    {
                        int iTmpInterval = int.Parse(sxSplitted[5]);
                        if (iTmpInterval > 0)
                        {
                            DBQuery.FeedGeneratorTable.SaveFeedGenerator(iInputId, sxSplitted[2], sxSplitted[3], sxSplitted[4], iTmpInterval);
                        }
                        else
                        {
                            sxReturn = "Interval tidak valid";
                        }
                    }
                }
                else
                {
                    sxReturn = "SQL is not valid!";
                }
                break;
            case "DEL":
                DBQuery.FeedGeneratorTable.DeleteFeedGenerator(iInputId);
                break;
            case "UPDATE":
                if (DBUtils.IsQueryValid(sxSplitted[2], sxSplitted[4], ref dt))
                {
                    sxReturn = VerifyTable(dt);

                    if (String.IsNullOrEmpty(sxReturn))
                    {
                        int iTmpInterval2 = int.Parse(sxSplitted[5]);
                        if (iTmpInterval2 > 0)
                        {
                            DBQuery.FeedGeneratorTable.UpdateFeedGenerator(iInputId, sxSplitted[2], sxSplitted[3], sxSplitted[4], iTmpInterval2);
                        }
                        else
                        {
                            sxReturn = "Interval tidak valid";
                        }
                    }
                }
                else
                {
                    sxReturn = "SQL is not valid!";
                }
                break;
            case "TOGGLE":
                DBQuery.FeedGeneratorTable.ToggleFeedGenerator(iInputId);
                break;
            case "EDIT":
                sxReturn = Build_Editor(iInputId);
                break;
        }
        return sxReturn;
    }

    private String Build_Editor(int iFeedGeneratorId)
    {
        String sxReturn = String.Empty;

        Cached.FeedGenerator ccTmp = new Cached.FeedGenerator();
        DataTable dtTmp = new DataView(
            ccTmp.GetFeedGeneratorList(true),
            "ID = " + iFeedGeneratorId.ToString(),
            String.Empty, DataViewRowState.CurrentRows).ToTable();

        if (dtTmp.Rows.Count > 0)
        {
            DataRow dr = dtTmp.Rows[0];
            sxReturn = AdminPageManager.BuildEditor_FeedGeneratorPopup(iFeedGeneratorId, dr["SQL"].ToString(),
                dr["DESCRIPTION"].ToString(), dr["NAME"].ToString(), dr["CONNECTION"].ToString(),
                Convert.ToInt32(dr["MINUTE_INTERVAL"]));
        }
        return sxReturn;
    }

    public String GetIcon(String sxActive)
    {
        return sxActive.ToUpper() == "Y" ? String.Empty : "0";
    }

    public String VerifyTable(DataTable dt)
    {
        String sxError = String.Empty;

        if (!dt.Columns.Contains("CONTENT"))
        {
            sxError += "Column \"CONTENT\" is not found from the query!";
        }

        if (!dt.Columns.Contains("POSTING_USER_ID"))
        {
            sxError += "Column \"POSTING_USER_ID\" is not found from the query!";
        }

        if (dt.Columns.Contains("POST_DATE"))
        {
            if (dt.Columns["POST_DATE"].DataType != typeof(DateTime))
            {
                sxError += "Column \"POST_DATE\" is not a Date column!";
            }
        }
        else
        {
            sxError += "Column \"POST_DATE\" is not found from the query!";
        }

        if (dt.Columns.Contains("CUSTOMER_ID"))
        {
            if (dt.Columns["CUSTOMER_ID"].DataType != typeof(Int32) && dt.Columns["CUSTOMER_ID"].DataType != typeof(Decimal))
            {
                sxError += "Column \"CUSTOMER_ID\" is not a number column!";
            }


        }


        return sxError;
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods


}
